<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Haproxy,负载均衡," />





  <link rel="alternate" href="/atom.xml" title="大牙的杂货铺" type="application/atom+xml" />






<meta name="description" content="官方文档：http://www.haproxy.org/#docs  配置HAProxy配置文件格式Haproxy的配置处理3类来主要参数来源：  最优先处理的命令行参数； global配置段，用于设定全局配置参数； proxy相关配置段，如defaults、listen、frontend和backend；  时间格式">
<meta name="keywords" content="Haproxy,负载均衡">
<meta property="og:type" content="article">
<meta property="og:title" content="Haproxy配置文件参数说明">
<meta property="og:url" content="http://linuxgeeks.github.io/2017/12/04/2017-12-04-223110-Haproxy配置文件参数说明/index.html">
<meta property="og:site_name" content="大牙的杂货铺">
<meta property="og:description" content="官方文档：http://www.haproxy.org/#docs  配置HAProxy配置文件格式Haproxy的配置处理3类来主要参数来源：  最优先处理的命令行参数； global配置段，用于设定全局配置参数； proxy相关配置段，如defaults、listen、frontend和backend；  时间格式一些包含了值的参数表示时间，如超时时长。这些值一般以毫秒为单位，但也可以使用其">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-01-30T14:14:04.901Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Haproxy配置文件参数说明">
<meta name="twitter:description" content="官方文档：http://www.haproxy.org/#docs  配置HAProxy配置文件格式Haproxy的配置处理3类来主要参数来源：  最优先处理的命令行参数； global配置段，用于设定全局配置参数； proxy相关配置段，如defaults、listen、frontend和backend；  时间格式一些包含了值的参数表示时间，如超时时长。这些值一般以毫秒为单位，但也可以使用其">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://linuxgeeks.github.io/2017/12/04/2017-12-04-223110-Haproxy配置文件参数说明/"/>





  <title>Haproxy配置文件参数说明 | 大牙的杂货铺</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
	
	<a href="https://github.com/mengdaya"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">大牙的杂货铺</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://linuxgeeks.github.io/2017/12/04/2017-12-04-223110-Haproxy配置文件参数说明/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="孟庆宇">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="大牙的杂货铺">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Haproxy配置文件参数说明</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-04T22:31:10+08:00">
                2017-12-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Haproxy/" itemprop="url" rel="index">
                    <span itemprop="name">Haproxy</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"> 本文总阅读量</i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  9,794
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  39
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>官方文档：<a href="http://www.haproxy.org/#docs" target="_blank" rel="noopener">http://www.haproxy.org/#docs</a></p>
</blockquote>
<h1 id="配置HAProxy"><a href="#配置HAProxy" class="headerlink" title="配置HAProxy"></a>配置HAProxy</h1><h2 id="配置文件格式"><a href="#配置文件格式" class="headerlink" title="配置文件格式"></a>配置文件格式</h2><p>Haproxy的配置处理3类来主要参数来源：</p>
<ul>
<li>最优先处理的命令行参数；</li>
<li><code>global</code>配置段，用于设定全局配置参数；</li>
<li>proxy相关配置段，如<code>defaults</code>、<code>listen</code>、<code>frontend</code>和<code>backend</code>；</li>
</ul>
<h3 id="时间格式"><a href="#时间格式" class="headerlink" title="时间格式"></a>时间格式</h3><p>一些包含了值的参数表示时间，如超时时长。这些值一般以毫秒为单位，但也可以使用其它的时间单位后缀。</p>
<ul>
<li>us: 微秒(microseconds)，即1/1000000秒；</li>
<li>ms: 毫秒(milliseconds)，即1/1000秒；</li>
<li>s: 秒(seconds)；</li>
<li>m: 分钟(minutes)；</li>
<li>h：小时(hours)；</li>
<li>d: 天(days)；</li>
</ul>
<h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><p>下面的例子配置了一个监听在所有接口的80端口上HTTP proxy服务，它转发所有的请求至后端监听在127.0.0.1:8000上的”server”。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">global</span><br><span class="line">      daemon</span><br><span class="line">      maxconn 25600</span><br><span class="line"></span><br><span class="line">  defaults</span><br><span class="line">      mode http</span><br><span class="line">      timeout connect 5000ms</span><br><span class="line">      timeout client 50000ms</span><br><span class="line">      timeout server 50000ms</span><br><span class="line"></span><br><span class="line">  frontend http-in</span><br><span class="line">      <span class="built_in">bind</span> *:80</span><br><span class="line">      default_backend servers</span><br><span class="line"></span><br><span class="line">  backend servers</span><br><span class="line">      server server1 127.0.0.1:8080 maxconn 32</span><br></pre></td></tr></table></figure>
<h2 id="全局配置"><a href="#全局配置" class="headerlink" title="全局配置"></a>全局配置</h2><p><code>global</code>配置中的参数为进程级别的参数，且通常与其运行的OS相关。</p>
<h3 id="进程管理及安全相关的参数"><a href="#进程管理及安全相关的参数" class="headerlink" title="进程管理及安全相关的参数"></a>进程管理及安全相关的参数</h3><ul>
<li><p><code>chroot &lt;jail dir&gt;</code>：修改haproxy的工作目录至指定的目录并在放弃权限之前执行chroot()操作，可以提升haproxy的安全级别，不过需要注意的是要确保指定的目录为空目录且任何用户均不能有写权限；一般情况下可以考虑不要，配置文件中注释掉或删掉即可；</p>
</li>
<li><p><code>daemon</code>：让haproxy以守护进程的方式工作于后台，其等同于“-D”选项的功能，当然，也可以在命令行中以“-db”选项将其禁用；</p>
</li>
<li><p><code>gid &lt;number&gt;</code>：以指定的GID运行haproxy，建议使用专用于运行haproxy的GID，以免因权限问题带来风险；</p>
</li>
<li><p><code>group &lt;group name&gt;</code>：同gid，不过指定的组名；</p>
</li>
<li><p><code>log  &lt;address&gt; &lt;facility&gt; [max level [min level]]</code>：定义全局的syslog服务器，最多可以定义两个；</p>
</li>
<li><p><code>log-send-hostname [&lt;string&gt;]</code>：在syslog信息的首部添加当前主机名，可以为“string”指定的名称，也可以缺省使用当前主机名；</p>
</li>
<li><p><code>nbproc &lt;number&gt;</code>：指定启动的haproxy进程个数，只能用于守护进程模式的haproxy；默认只启动一个进程，鉴于调试困难等多方面的原因，一般只在单进程仅能打开少数文件描述符的场景中才使用多进程模式；</p>
</li>
<li><p><code>pidfile：pid文件</code>，也可以使用命令行中”-p”选项指定pid文件</p>
</li>
<li><p><code>uid</code>：以指定的UID身份运行haproxy进程；</p>
</li>
<li><p><code>ulimit-n</code>：设定每进程所能够打开的最大文件描述符数目，默认情况下其会自动进行计算，因此不推荐修改此选项；</p>
</li>
<li><p><code>user</code>：同uid，但使用的是用户名；</p>
</li>
<li><p><code>stats</code>：是否开启Haproxy 状态信息页面</p>
</li>
<li><p><code>node</code>：定义当前节点的名称，用于HA场景中多haproxy进程共享同一个IP地址时；</p>
</li>
<li><p><code>description</code>：当前实例的描述信息；</p>
</li>
</ul>
<h3 id="性能调整相关的参数"><a href="#性能调整相关的参数" class="headerlink" title="性能调整相关的参数"></a>性能调整相关的参数</h3><ul>
<li><p><code>maxconn &lt;number&gt;</code>：设定每个haproxy进程所接受的最大并发连接数，其等同于命令行选项“-n”；“ulimit -n”自动计算的结果正是参照此参数设定的；</p>
</li>
<li><p><code>maxpipes &lt;number&gt;</code>：haproxy使用pipe完成基于内核的tcp报文重组，此选项则用于设定每进程所允许使用的最大pipe个数；每个pipe会打开两个文件描述符，因此，“ulimit -n”自动计算时会根据需要调大此值；默认为maxconn/4，其通常会显得过大；</p>
</li>
<li><p><code>noepoll</code>：在Linux系统上禁用epoll机制；</p>
</li>
<li><p><code>nokqueue</code>：在BSE系统上禁用kqueue机制；</p>
</li>
<li><p><code>nopoll</code>：禁用poll机制；</p>
</li>
<li><p><code>nosepoll</code>：在Linux禁用启发式epoll机制；</p>
</li>
<li><p><code>nosplice</code>：禁止在Linux套接字上使用内核tcp重组，这会导致更多的recv/send系统调用；不过，在Linux 2.6.25-28系列的内核上，tcp重组功能有bug存在；</p>
</li>
<li><p><code>spread-checks &lt;0..50, in percent&gt;</code>：在haproxy后端有着众多服务器的场景中，在精确的时间间隔后统一对众服务器进行健康状况检查可能会带来意外问题；此选项用于将其检查的时间间隔长度上增加或减小一定的随机时长；</p>
</li>
<li><p><code>tune.bufsize &lt;number&gt;</code>：设定buffer的大小，同样的内存条件大小，较小的值可以让haproxy有能力接受更多的并发连接，较大的值可以让某些应用程序使用较大的cookie信息；默认为16384，其可以在编译时修改，不过强烈建议使用默认值；</p>
</li>
<li><p><code>tune.chksize &lt;number&gt;</code>：设定检查缓冲区的大小，单位为字节；更大的值有助于在较大的页面中完成基于字符串或模式的文本查找，但也会占用更多的系统资源；不建议修改；</p>
</li>
<li><p><code>tune.maxaccept &lt;number&gt;</code>：设定haproxy进程内核调度运行时一次性可以接受的连接的个数，较大的值可以带来较大的吞吐率，默认在单进程模式下为100，多进程模式下为8，设定为-1可以禁止此限制；一般不建议修改；</p>
</li>
<li><p><code>tune.maxpollevents  &lt;number&gt;</code>：设定一次系统调用可以处理的事件最大数，默认值取决于OS；其值小于200时可节约带宽，但会略微增大网络延迟，而大于200时会降低延迟，但会稍稍增加网络带宽的占用量；</p>
</li>
<li><p><code>tune.maxrewrite &lt;number&gt;</code>：设定为首部重写或追加而预留的缓冲空间，建议使用1024左右的大小；在需要使用更大的空间时，haproxy会自动增加其值；</p>
</li>
</ul>
<h3 id="Debug相关的参数"><a href="#Debug相关的参数" class="headerlink" title="Debug相关的参数"></a>Debug相关的参数</h3><ul>
<li><p>debug </p>
</li>
<li><p>quiet</p>
</li>
</ul>
<h2 id="代理"><a href="#代理" class="headerlink" title="代理"></a>代理</h2><p>代理相关的配置可以如下配置段中。</p>
<ul>
<li><p><code>defaults &lt;name&gt;</code><br>“defaults”段用于为所有其它配置段提供默认参数，这配置默认配置参数可由下一个“defaults”所重新设定。</p>
</li>
<li><p><code>frontend &lt;name&gt;</code><br>“frontend”段用于定义一系列监听的套接字，这些套接字可接受客户端请求并与之建立连接。</p>
</li>
<li><p><code>backend  &lt;name&gt;</code><br>“backend”段用于定义一系列“后端”服务器，代理将会将对应客户端的请求转发至这些服务器。</p>
</li>
<li><p><code>listen   &lt;name&gt;</code><br>“listen”段通过关联“前端”和“后端”定义了一个完整的代理，通常只对TCP流量有用。</p>
</li>
</ul>
<p> 所有代理的名称只能使用大写字母、小写字母、数字、-(中线)、_(下划线)、.(点号)和:(冒号)。此外，ACL名称会区分字母大小写。</p>
<h1 id="配置文件中的关键字参考"><a href="#配置文件中的关键字参考" class="headerlink" title="配置文件中的关键字参考"></a>配置文件中的关键字参考</h1><h2 id="balance"><a href="#balance" class="headerlink" title="balance"></a>balance</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">balance &lt;algorithm&gt; [ &lt;arguments&gt; ]</span><br><span class="line">balance url_param &lt;param&gt; [check_post [&lt;max_wait&gt;]]</span><br></pre></td></tr></table></figure>
<p>定义负载均衡算法，可用于“defaults”、“listen”和“backend”。<code>&lt;algorithm&gt;</code>用于在负载均衡场景中挑选一个server，其仅应用于持久信息不可用的条件下或需要将一个连接重新派发至另一个服务器时。支持的算法有：</p>
<ul>
<li><p>roundrobin</p>
<ul>
<li>基于权重进行轮叫，在服务器的处理时间保持均匀分布时，这是最平衡、最公平的算法</li>
<li>此算法是动态的，这表示其权重可以在运行时进行调整，不过，在设计上，每个后端服务器仅能最多接受4128个连接；</li>
</ul>
</li>
<li><p>static-rr</p>
<ul>
<li>基于权重进行轮叫，与roundrobin类似，但是为静态方法，在运行时调整其服务器权重不会生效；不过，其在后端服务器连接数上没有限制；</li>
</ul>
</li>
<li><p>leastconn</p>
<ul>
<li>新的连接请求被派发至具有最少连接数目的后端服务器</li>
<li>在有着较长时间会话的场景中推荐使用此算法，如LDAP、SQL等，其并不太适用于较短会话的应用层协议，如HTTP；</li>
<li>此算法是动态的，可以在运行时调整其权重；</li>
</ul>
</li>
<li><p>source</p>
<ul>
<li>将请求的源地址进行hash运算，并由后端服务器的权重总数相除后派发至某匹配的服务器；</li>
<li>这可以使得同一个客户端IP的请求始终被派发至某特定的服务器；</li>
<li>不过，当服务器权重总数发生变化时，如某服务器宕机或添加了新的服务器，许多客户端的请求可能会被派发至与此前请求不同的服务器；</li>
<li>常用于负载均衡无cookie功能的基于TCP的协议；</li>
<li>其默认为静态，不过也可以使用hash-type修改此特性；</li>
</ul>
</li>
</ul>
<ul>
<li><p>uri</p>
<ul>
<li>对URI的左半部分(“问题”标记之前的部分)或整个URI进行hash运算，并由服务器的总权重相除后派发至某匹配的服务器；</li>
<li>这可以使得对同一个URI的请求总是被派发至某特定的服务器，除非服务器的权重总数发生了变化；此算法常用于代理缓存或反病毒代理以提高缓存的命中率；</li>
<li>需要注意的是，此算法仅应用于HTTP后端服务器场景；</li>
<li>其默认为静态算法，不过也可以使用hash-type修改此特性；</li>
</ul>
</li>
<li><p>url_param</p>
<ul>
<li>通过<code>&lt;argument&gt;</code>为URL指定的参数在每个HTTP GET请求中将会被检索；</li>
<li>如果找到了指定的参数且其通过等于号“=”被赋予了一个值，那么此值将被执行hash运算并被服务器的总权重相除后派发至某匹配的服务器；</li>
<li>此算法可以通过追踪请求中的用户标识进而确保同一个用户ID的请求将被送往同一个特定的服务器，除非服务器的总权重发生了变化；</li>
<li>如果某请求中没有出现指定的参数或其没有有效值，则使用轮叫算法对相应请求进行调度；</li>
<li>此算法默认为静态的，不过其也可以使用hash-type修改此特性；</li>
</ul>
</li>
<li><p><code>hdr(&lt;name&gt;)</code></p>
<ul>
<li>对于每个HTTP请求，通过<name>指定的HTTP首部将会被检索；</name></li>
<li>如果相应的首部没有出现或其没有有效值，则使用轮叫算法对相应请求进行调度；</li>
<li>其有一个可选选项“use_domain_only”，可在指定检索类似Host类的首部时仅计算域名部分(比如通过www.magedu.com来说，仅计算magedu字符串的hash值)以降低hash算法的运算量；</li>
<li>此算法默认为静态的，不过其也可以使用hash-type修改此特性；</li>
</ul>
</li>
</ul>
<h2 id="bind"><a href="#bind" class="headerlink" title="bind"></a>bind</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">bind</span> [&lt;address&gt;]:&lt;port_range&gt; [, ...]</span><br><span class="line"><span class="built_in">bind</span> [&lt;address&gt;]:&lt;port_range&gt; [, ...] interface &lt;interface&gt;</span><br></pre></td></tr></table></figure>
<p>此指令仅能用于frontend和listen区段，用于定义一个或几个监听的套接字。</p>
<ul>
<li><code>&lt;address&gt;</code>：可选选项，其可以为主机名、IPv4地址、IPv6地址或<em>；省略此选项、将其指定为</em>或0.0.0.0时，将监听当前系统的所有IPv4地址；</li>
<li><code>&lt;port_range&gt;</code>：可以是一个特定的TCP端口，也可是一个端口范围(如5005-5010)，代理服务器将通过指定的端口来接收客户端请求；需要注意的是，每组监听的套接字<address:port>在同一个实例上只能使用一次，而且小于1024的端口需要有特定权限的用户才能使用，这可能需要通过uid参数来定义；</address:port></li>
<li><code>&lt;interface&gt;</code>：指定物理接口的名称，仅能在Linux系统上使用；其不能使用接口别名，而仅能使用物理接口名称，而且只有管理有权限指定绑定的物理接口；</li>
</ul>
<h2 id="mode"><a href="#mode" class="headerlink" title="mode"></a>mode</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mode &#123; tcp|http|health &#125;</span><br></pre></td></tr></table></figure>
<p>设定实例的运行模式或协议。当实现内容交换时，前端和后端必须工作于同一种模式(一般说来都是HTTP模式)，否则将无法启动实例。</p>
<ul>
<li>tcp：实例运行于纯TCP模式，在客户端和服务器端之间将建立一个全双工的连接，且不会对7层报文做任何类型的检查；此为默认模式，通常用于SSL、SSH、SMTP等应用；</li>
<li>http：实例运行于HTTP模式，客户端请求在转发至后端服务器之前将被深度分析，所有不与RFC格式兼容的请求都会被拒绝；</li>
<li>health：实例工作于health模式，其对入站请求仅响应“OK”信息并关闭连接，且不会记录任何日志信息；此模式将用于响应外部组件的健康状态检查请求；目前业讲，此模式已经废弃，因为tcp或http模式中的monitor关键字可完成类似功能；</li>
</ul>
<h2 id="hash-type"><a href="#hash-type" class="headerlink" title="hash-type"></a>hash-type</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">hash</span>-type &lt;method&gt;</span><br></pre></td></tr></table></figure>
<p>定义用于将hash码映射至后端服务器的方法；其不能用于frontend区段；可用方法有map-based和consistent，在大多数场景下推荐使用默认的map-based方法。</p>
<ul>
<li>map-based：hash表是一个包含了所有在线服务器的静态数组。其hash值将会非常平滑，会将权重考虑在列，但其为静态方法，对在线服务器的权重进行调整将不会生效，这意味着其不支持慢速启动。此外，挑选服务器是根据其在数组中的位置进行的，因此，当一台服务器宕机或添加了一台新的服务器时，大多数连接将会被重新派发至一个与此前不同的服务器上，对于缓存服务器的工作场景来说，此方法不甚适用。</li>
<li>consistent：hash表是一个由各服务器填充而成的树状结构；基于hash键在hash树中查找相应的服务器时，最近的服务器将被选中。此方法是动态的，支持在运行时修改服务器权重，因此兼容慢速启动的特性。添加一个新的服务器时，仅会对一小部分请求产生影响，因此，尤其适用于后端服务器为cache的场景。不过，此算法不甚平滑，派发至各服务器的请求未必能达到理想的均衡效果，因此，可能需要不时的调整服务器的权重以获得更好的均衡性。</li>
</ul>
<h2 id="log"><a href="#log" class="headerlink" title="log"></a>log</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">log</span> global</span><br><span class="line"><span class="built_in">log</span> &lt;address&gt; &lt;facility&gt; [&lt;level&gt; [&lt;minlevel&gt;]]</span><br></pre></td></tr></table></figure>
<p>为每个实例启用事件和流量日志，因此可用于所有区段。每个实例最多可以指定两个log参数，不过，如果使用了“log global”且”global”段已经定了两个log参数时，多余了log参数将被忽略。</p>
<ul>
<li>global：当前实例的日志系统参数同”global”段中的定义时，将使用此格式；每个实例仅能定义一次“log global”语句，且其没有任何额外参数；</li>
<li><code>&lt;address&gt;</code>：定义日志发往的位置，其格式之一可以为<ipv4_address:port>，其中的port为UDP协议端口，默认为514；格式之二为Unix套接字文件路径，但需要留心chroot应用及用户的读写权限；<ul>
<li><code>&lt;facility&gt;</code>：可以为syslog系统的标准facility之一；</li>
<li><code>&lt;level&gt;</code>：定义日志级别，即输出信息过滤器，默认为所有信息；指定级别时，所有等于或高于此级别的日志信息将会被发送；</li>
</ul>
</ipv4_address:port></li>
</ul>
<h2 id="maxconn"><a href="#maxconn" class="headerlink" title="maxconn"></a>maxconn</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">maxconn &lt;conns&gt;</span><br></pre></td></tr></table></figure>
<p>设定一个前端的最大并发连接数，因此，其不能用于backend区段。对于大型站点来说，可以尽可能提高此值以便让haproxy管理连接队列，从而避免无法应答用户请求。当然，此最大值不能超出“global”段中的定义。此外，需要留心的是，haproxy会为每个连接维持两个缓冲，每个缓冲的大小为8KB，再加上其它的数据，每个连接将大约占用17KB的RAM空间。这意味着经过适当优化后，有着1GB的可用RAM空间时将能维护40000-50000并发连接。</p>
<p>如果为<conns>指定了一个过大值，极端场景下，其最终占据的空间可能会超出当前主机的可用内存，这可能会带来意想不到的结果；因此，将其设定了一个可接受值方为明智决定。其默认为2000。</conns></p>
<h2 id="default-backend"><a href="#default-backend" class="headerlink" title="default_backend"></a>default_backend</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">default_backend &lt;backend&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>在没有匹配的”use_backend”规则时为实例指定使用的默认后端，因此，其不可应用于backend区段</li>
<li>在”frontend”和”backend”之间进行内容交换时，通常使用”use-backend”定义其匹配规则；而没有被规则匹配到的请求将由此参数指定的后端接收</li>
<li><p><backend>：指定使用的后端的名称；</backend></p>
</li>
<li><p>使用案例：</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">use_backend     dynamic  <span class="keyword">if</span>  url_dyn</span><br><span class="line">use_backend     static   <span class="keyword">if</span>  url_css url_img extension_img</span><br><span class="line">default_backend dynamic</span><br></pre></td></tr></table></figure>
<h2 id="server"><a href="#server" class="headerlink" title="server"></a>server</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">server &lt;name&gt; &lt;address&gt;[:port] [param*]</span><br></pre></td></tr></table></figure>
<p>为后端声明一个server，因此，不能用于defaults和frontend区段。</p>
<ul>
<li><code>&lt;name&gt;</code>：为此服务器指定的内部名称，其将出现在日志及警告信息中；如果设定了”http-send-server-name”，它还将被添加至发往此服务器的请求首部中；</li>
<li><code>&lt;address&gt;</code>：此服务器的的IPv4地址，也支持使用可解析的主机名，只不过在启动时需要解析主机名至相应的IPv4地址；</li>
<li><code>[:port]</code>：指定将连接请求所发往的此服务器时的目标端口，其为可选项；未设定时，将使用客户端请求时的同一相端口；</li>
<li><p><code>[param*]</code>：为此服务器设定的一系参数；其可用的参数非常多，具体请参考官方文档中的说明，下面仅说明几个常用的参数；</p>
</li>
<li><p>服务器或默认服务器参数：</p>
<ul>
<li><code>backup</code>：设定为备用服务器，仅在负载均衡场景中的其它server均不可用于启用此server；</li>
<li><code>check</code>：启动对此server执行健康状态检查，其可以借助于额外的其它参数完成更精细的设定，如：<ul>
<li><code>inter &lt;delay&gt;</code>：设定健康状态检查的时间间隔，单位为毫秒，默认为2000；也可以使用fastinter和downinter来根据服务器端状态优化此时间延迟；</li>
<li><code>rise &lt;count&gt;</code>：设定健康状态检查中，某离线的server从离线状态转换至正常状态需要成功检查的次数；</li>
<li><code>fall &lt;count&gt;</code>：确认server从正常状态转换为不可用状态需要检查的次数；</li>
</ul>
</li>
<li><code>cookie &lt;value&gt;</code>：为指定server设定cookie值，此处指定的值将在请求入站时被检查，第一次为此值挑选的server将在后续的请求中被选中，其目的在于实现持久连接的功能；</li>
<li><code>maxconn &lt;maxconn&gt;</code>：指定此服务器接受的最大并发连接数；如果发往此服务器的连接数目高于此处指定的值，其将被放置于请求队列，以等待其它连接被释放；</li>
<li><code>maxqueue &lt;maxqueue&gt;</code>：设定请求队列的最大长度；</li>
<li><code>observe &lt;mode&gt;</code>：通过观察服务器的通信状况来判定其健康状态，默认为禁用，其支持的类型有“layer4”和“layer7”，“layer7”仅能用于http代理场景；</li>
<li><p><code>redir &lt;prefix&gt;</code>：启用重定向功能，将发往此服务器的GET和HEAD请求均以302状态码响应；需要注意的是，在prefix后面不能使用/，且不能使用相对地址，以免造成循环；例如：<code>server srv1 172.16.100.6:80 redir http://imageserver.magedu.com check</code></p>
</li>
<li><p><code>weight &lt;weight&gt;</code>：权重，默认为1，最大值为256，0表示不参与负载均衡；</p>
</li>
</ul>
</li>
<li><p>检查方法：</p>
<ul>
<li><code>option httpchk</code></li>
<li><code>option httpchk &lt;uri&gt;</code></li>
<li><code>option httpchk &lt;method&gt; &lt;uri&gt;</code></li>
<li><code>option httpchk &lt;method&gt; &lt;uri&gt; &lt;version&gt;</code>：不能用于frontend段，例如：</li>
</ul>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">backend https_relay</span><br><span class="line">    mode tcp</span><br><span class="line">    option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www.magedu.com</span><br><span class="line">    server apache1 192.168.1.1:443 check port 80</span><br></pre></td></tr></table></figure>
<ul>
<li>使用案例：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server first  172.16.100.7:1080 cookie first  check inter 1000</span><br><span class="line">server second 172.16.100.8:1080 cookie second check inter 1000</span><br></pre></td></tr></table></figure>
<h2 id="capture-request-header"><a href="#capture-request-header" class="headerlink" title="capture request header"></a>capture request header</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">capture request header &lt;name&gt; len &lt;length&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>捕获并记录指定的请求首部最近一次出现时的第一个值，仅能用于“frontend”和“listen”区段。捕获的首部值使用花括号{}括起来后添加进日志中。如果需要捕获多个首部值，它们将以指定的次序出现在日志文件中，并以竖线“|”作为分隔符。不存在的首部记录为空字符串，最常需要捕获的首部包括在虚拟主机环境中使用的“Host”、上传请求首部中的“Content-length”、快速区别真实用户和网络机器人的“User-agent”，以及代理环境中记录真实请求来源的“X-Forward-For”。</p>
<ul>
<li><code>&lt;name&gt;</code>：要捕获的首部的名称，此名称不区分字符大小写，但建议与它们出现在首部中的格式相同，比如大写首字母。需要注意的是，记录在日志中的是首部对应的值，而非首部名称。</li>
<li><code>&lt;length&gt;</code>：指定记录首部值时所记录的精确长度，超出的部分将会被忽略。</li>
</ul>
</li>
<li><p>可以捕获的请求首部的个数没有限制，但每个捕获最多只能记录64个字符。为了保证同一个frontend中日志格式的统一性，首部捕获仅能在frontend中定义。</p>
</li>
</ul>
<h2 id="capture-response-header"><a href="#capture-response-header" class="headerlink" title="capture response header"></a>capture response header</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">capture response header &lt;name&gt; len &lt;length&gt;</span><br></pre></td></tr></table></figure>
<p>捕获并记录响应首部，其格式和要点同请求首部。</p>
<h2 id="stats-enable"><a href="#stats-enable" class="headerlink" title="stats enable"></a>stats enable</h2><ul>
<li><p>启用基于程序编译时默认设置的统计报告，不能用于“frontend”区段。只要没有另外的其它设定，它们就会使用如下的配置：</p>
<ul>
<li>stats uri   : /haproxy?stats</li>
<li>stats realm : “HAProxy Statistics”</li>
<li>stats auth  : no authentication</li>
<li>stats scope : no restriction</li>
</ul>
</li>
<li><p>尽管“stats enable”一条就能够启用统计报告，但还是建议设定其它所有的参数，以免其依赖于默认设定而带来非期后果。下面是一个配置案例。</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">backend public_www</span><br><span class="line">  server websrv1 172.16.100.11:80</span><br><span class="line">  stats <span class="built_in">enable</span></span><br><span class="line">  stats hide-version</span><br><span class="line">  stats scope   .</span><br><span class="line">  stats uri     /haproxyadmin?stats</span><br><span class="line">  stats realm   Haproxy\ Statistics</span><br><span class="line">  stats auth    statsadmin:password</span><br><span class="line">  stats auth    statsmaster:password</span><br></pre></td></tr></table></figure>
<h2 id="stats-hide-version"><a href="#stats-hide-version" class="headerlink" title="stats hide-version"></a>stats hide-version</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stats hide-version</span><br></pre></td></tr></table></figure>
<p>启用统计报告并隐藏HAProxy版本报告，不能用于“frontend”区段。默认情况下，统计页面会显示一些有用信息，包括HAProxy的版本号，然而，向所有人公开HAProxy的精确版本号是非常有风险的，因为它能帮助恶意用户快速定位版本的缺陷和漏洞。尽管“stats hide-version”一条就能够启用统计报告，但还是建议设定其它所有的参数，以免其依赖于默认设定而带来非期后果。具体请参照“stats enable”一节的说明。</p>
<h2 id="stats-realm"><a href="#stats-realm" class="headerlink" title="stats realm"></a>stats realm</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stats realm &lt;realm&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>启用统计报告并高精认证领域，不能用于“frontend”区段。haproxy在读取realm时会将其视作一个单词，因此，中间的任何空白字符都必须使用反斜线进行转义。此参数仅在与“stats auth”配置使用时有意义。</p>
</li>
<li><p><code>&lt;realm&gt;</code>：实现HTTP基本认证时显示在浏览器中的领域名称，用于提示用户输入一个用户名和密码。</p>
</li>
<li><p>尽管“stats realm”一条就能够启用统计报告，但还是建议设定其它所有的参数，以免其依赖于默认设定而带来非期后果。具体请参照“stats enable”一节的说明。</p>
</li>
</ul>
<h2 id="stats-scope"><a href="#stats-scope" class="headerlink" title="stats scope"></a>stats scope</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stats scope &#123; &lt;name&gt; | <span class="string">"."</span> &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>启用统计报告并限定报告的区段，不能用于“frontend”区段。当指定此语句时，统计报告将仅显示其列举出区段的报告信息，所有其它区段的信息将被隐藏。如果需要显示多个区段的统计报告，此语句可以定义多次。需要注意的是，区段名称检测仅仅是以字符串比较的方式进行，它不会真检测指定的区段是否真正存在。</p>
<ul>
<li><code>&lt;name&gt;</code>：可以是一个“listen”、“frontend”或“backend”区段的名称，而“.”则表示stats scope语句所定义的当前区段。</li>
</ul>
</li>
<li><p>尽管“stats scope”一条就能够启用统计报告，但还是建议设定其它所有的参数，以免其依赖于默认设定而带来非期后果。下面是一个配置案例。</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">backend private_monitoring</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats uri     /haproxyadmin?stats</span><br><span class="line">    stats refresh 10s</span><br></pre></td></tr></table></figure>
<h2 id="stats-auth"><a href="#stats-auth" class="headerlink" title="stats auth"></a>stats auth</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stats auth &lt;user&gt;:&lt;passwd&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>启用带认证的统计报告功能并授权一个用户帐号，其不能用于“frontend”区段。</p>
<ul>
<li><code>&lt;user&gt;</code>：授权进行访问的用户名；</li>
<li><code>&lt;passwd&gt;</code>：此用户的访问密码，明文格式；</li>
</ul>
</li>
<li><p>此语句将基于默认设定启用统计报告功能，并仅允许其定义的用户访问，其也可以定义多次以授权多个用户帐号。可以结合“stats realm”参数在提示用户认证时给出一个领域说明信息。在使用非法用户访问统计功能时，其将会响应一个“401 Forbidden”页面。其认证方式为HTTP Basic认证，密码传输会以明文方式进行，因此，配置文件中也使用明文方式存储以说明其非保密信息故此不能相同于其它关键性帐号的密码。</p>
</li>
<li><p>尽管“stats auth”一条就能够启用统计报告，但还是建议设定其它所有的参数，以免其依赖于默认设定而带来非期后果。</p>
</li>
</ul>
<h2 id="stats-admin"><a href="#stats-admin" class="headerlink" title="stats admin"></a>stats admin</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stats admin &#123; <span class="keyword">if</span> | unless &#125; &lt;cond&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>在指定的条件满足时启用统计报告页面的管理级别功能，它允许通过web接口启用或禁用服务器，不过，基于安全的角度考虑，统计报告页面应该尽可能为只读的。此外，如果启用了HAProxy的多进程模式，启用此管理级别将有可能导致异常行为。</p>
</li>
<li><p>目前来说，POST请求方法被限制于仅能使用缓冲区减去保留部分之外的空间，因此，服务器列表不能过长，否则，此请求将无法正常工作。因此，建议一次仅调整少数几个服务器。下面是两个案例，第一个限制了仅能在本机打开报告页面时启用管理级别功能，第二个定义了仅允许通过认证的用户使用管理级别功能。</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">backend stats_localhost</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats admin <span class="keyword">if</span> LOCALHOST</span><br><span class="line"></span><br><span class="line">backend stats_auth</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats auth  haproxyadmin:password</span><br><span class="line">    stats admin <span class="keyword">if</span> TRUE</span><br></pre></td></tr></table></figure>
<h2 id="option-httplog"><a href="#option-httplog" class="headerlink" title="option httplog"></a>option httplog</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">option httplog [ clf ]</span><br></pre></td></tr></table></figure>
<ul>
<li><p>启用记录HTTP请求、会话状态和计时器的功能。</p>
<ul>
<li>clf：使用CLF格式来代替HAProxy默认的HTTP格式，通常在使用仅支持CLF格式的特定日志分析器时才需要使用此格式。</li>
</ul>
</li>
<li><p>默认情况下，日志输入格式非常简陋，因为其仅包括源地址、目标地址和实例名称，而“option httplog”参数将会使得日志格式变得丰富许多，其通常包括但不限于HTTP请求、连接计时器、会话状态、连接数、捕获的首部及cookie、“frontend”、“backend”及服务器名称，当然也包括源地址和端口号等。</p>
</li>
</ul>
<h2 id="option-logasap"><a href="#option-logasap" class="headerlink" title="option logasap"></a>option logasap</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">option logasap</span><br><span class="line">no option logasap</span><br></pre></td></tr></table></figure>
<ul>
<li><p>启用或禁用提前将HTTP请求记入日志，不能用于“backend”区段。</p>
</li>
<li><p>默认情况下，HTTP请求是在请求结束时进行记录以便能将其整体传输时长和字节数记入日志，由此，传较大的对象时，其记入日志的时长可能会略有延迟。“option logasap”参数能够在服务器发送complete首部时即时记录日志，只不过，此时将不记录整体传输时长和字节数。此情形下，捕获“Content-Length”响应首部来记录传输的字节数是一个较好选择。下面是一个例子。</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">listen http_proxy 0.0.0.0:80</span><br><span class="line">    mode http</span><br><span class="line">    option httplog</span><br><span class="line">    option logasap</span><br><span class="line">    <span class="built_in">log</span> 172.16.100.9 local2</span><br></pre></td></tr></table></figure>
<h2 id="option-forwardfor"><a href="#option-forwardfor" class="headerlink" title="option forwardfor"></a>option forwardfor</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">option forwardfor [ except &lt;network&gt; ] [ header &lt;name&gt; ] [ <span class="keyword">if</span>-none ]</span><br></pre></td></tr></table></figure>
<ul>
<li><p>允许在发往服务器的请求首部中插入“X-Forwarded-For”首部。</p>
<ul>
<li><code>&lt;network&gt;</code>：可选参数，当指定时，源地址为匹配至此网络中的请求都禁用此功能。</li>
<li><code>&lt;name&gt;</code>：可选参数，可使用一个自定义的首部，如“X-Client”来替代“X-Forwarded-For”。有些独特的web服务器的确需要用于一个独特的首部。</li>
<li><code>if-none</code>：仅在此首部不存在时才将其添加至请求报文问道中。</li>
</ul>
</li>
<li><p>HAProxy工作于反向代理模式，其发往服务器的请求中的客户端IP均为HAProxy主机的地址而非真正客户端的地址，这会使得服务器端的日志信息记录不了真正的请求来源，“X-Forwarded-For”首部则可用于解决此问题。HAProxy可以向每个发往服务器的请求上添加此首部，并以客户端IP为其value。</p>
</li>
<li><p>需要注意的是，HAProxy工作于隧道模式，其仅检查每一个连接的第一个请求，因此，仅第一个请求报文被附加此首部。如果想为每一个请求都附加此首部，请确保同时使用了“option httpclose”、“option forceclose”和“option http-server-close”几个option。</p>
</li>
</ul>
<p>-下面是一个例子。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">frontend www</span><br><span class="line">    mode http</span><br><span class="line">    option forwardfor except 127.0.0.1</span><br></pre></td></tr></table></figure>
<h2 id="errorfile"><a href="#errorfile" class="headerlink" title="errorfile"></a>errorfile</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">errorfile &lt;code&gt; &lt;file&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>在用户请求不存在的页面时，返回一个页面文件给客户端而非由haproxy生成的错误代码；可用于所有段中。</p>
<ul>
<li><code>&lt;code&gt;</code>：指定对HTTP的哪些状态码返回指定的页面；这里可用的状态码有200、400、403、408、500、502、503和504；</li>
<li><code>&lt;file&gt;</code>：指定用于响应的页面文件；</li>
</ul>
</li>
<li><p>例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">errorfile 400 /etc/haproxy/errorpages/400badreq.http</span><br><span class="line">errorfile 403 /etc/haproxy/errorpages/403forbid.http</span><br><span class="line">errorfile 503 /etc/haproxy/errorpages/503sorry.http</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="errorloc-和-errorloc302"><a href="#errorloc-和-errorloc302" class="headerlink" title="errorloc 和 errorloc302"></a>errorloc 和 errorloc302</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">errorloc &lt;code&gt; &lt;url&gt;</span><br><span class="line">errorloc302 &lt;code&gt; &lt;url&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>请求错误时，返回一个HTTP重定向至某URL的信息；可用于所有配置段中。</p>
<ul>
<li><code>&lt;code&gt;</code>：指定对HTTP的哪些状态码返回指定的页面；这里可用的状态码有200、400、403、408、500、502、503和504；</li>
<li><code>&lt;url&gt;</code>：Location首部中指定的页面位置的具体路径，可以是在当前服务器上的页面的相对路径，也可以使用绝对路径；需要注意的是，如果URI自身错误时产生某特定状态码信息的话，有可能会导致循环定向；</li>
</ul>
</li>
<li><p>需要留意的是，这两个关键字都会返回302状态吗，这将使得客户端使用同样的HTTP方法获取指定的URL，对于非GET法的场景(如POST)来说会产生问题，因为返回客户的URL是不允许使用GET以外的其它方法的。如果的确有这种问题，可以使用errorloc303来返回303状态码给客户端。</p>
</li>
</ul>
<h2 id="errorloc303"><a href="#errorloc303" class="headerlink" title="errorloc303"></a>errorloc303</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">errorloc303 &lt;code&gt; &lt;url&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>请求错误时，返回一个HTTP重定向至某URL的信息给客户端；可用于所有配置段中。</p>
<ul>
<li><code>&lt;code&gt;</code>：指定对HTTP的哪些状态码返回指定的页面；这里可用的状态码有400、403、408、500、502、503和504；</li>
<li><code>&lt;url&gt;</code>：Location首部中指定的页面位置的具体路径，可以是在当前服务器上的页面的相对路径，也可以使用绝对路径；需要注意的是，如果URI自身错误时产生某特定状态码信息的话，有可能会导致循环定向；</li>
</ul>
</li>
<li><p>例如：</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">backend webserver</span><br><span class="line">  server 172.16.100.6 172.16.100.6:80 check maxconn 3000 cookie srv01</span><br><span class="line">  server 172.16.100.7 172.16.100.7:80 check maxconn 3000 cookie srv02</span><br><span class="line">  errorloc 403 /etc/haproxy/errorpages/sorry.htm</span><br><span class="line">  errorloc 503 /etc/haproxy/errorpages/sorry.htm</span><br></pre></td></tr></table></figure>
<h1 id="配置案例"><a href="#配置案例" class="headerlink" title="配置案例"></a>配置案例</h1><h2 id="http服务器配置示例"><a href="#http服务器配置示例" class="headerlink" title="http服务器配置示例"></a>http服务器配置示例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#---------------------------------------------------------------------</span></span><br><span class="line"><span class="comment"># Global settings</span></span><br><span class="line"><span class="comment">#---------------------------------------------------------------------</span></span><br><span class="line">global</span><br><span class="line">    <span class="comment"># to have these messages end up in /var/log/haproxy.log you will</span></span><br><span class="line">    <span class="comment"># need to:</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment"># 1) configure syslog to accept network log events.  This is done</span></span><br><span class="line">    <span class="comment">#    by adding the '-r' option to the SYSLOGD_OPTIONS in</span></span><br><span class="line">    <span class="comment">#    /etc/sysconfig/syslog</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment"># 2) configure local2 events to go to the /var/log/haproxy.log</span></span><br><span class="line">    <span class="comment">#   file. A line like the following can be added to</span></span><br><span class="line">    <span class="comment">#   /etc/sysconfig/syslog</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment">#    local2.*                       /var/log/haproxy.log</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="built_in">log</span>         127.0.0.1 local2</span><br><span class="line"></span><br><span class="line">    chroot      /var/lib/haproxy</span><br><span class="line">    pidfile     /var/run/haproxy.pid</span><br><span class="line">    maxconn     4000</span><br><span class="line">    user        haproxy</span><br><span class="line">    group       haproxy</span><br><span class="line">    daemon</span><br><span class="line"></span><br><span class="line">defaults</span><br><span class="line">    mode                    http</span><br><span class="line">    <span class="built_in">log</span>                     global</span><br><span class="line">    option                  httplog</span><br><span class="line">    option                  dontlognull</span><br><span class="line">    option http-server-close</span><br><span class="line">    option forwardfor       except 127.0.0.0/8</span><br><span class="line">    option                  redispatch</span><br><span class="line">    retries                 3</span><br><span class="line">    timeout http-request    10s</span><br><span class="line">    timeout queue           1m</span><br><span class="line">    timeout connect         10s</span><br><span class="line">    timeout client          1m</span><br><span class="line">    timeout server          1m</span><br><span class="line">    timeout http-keep-alive 10s</span><br><span class="line">    timeout check           10s</span><br><span class="line">    maxconn                 30000</span><br><span class="line"></span><br><span class="line">listen stats</span><br><span class="line">    mode http</span><br><span class="line">    <span class="built_in">bind</span> 0.0.0.0:1080</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats hide-version</span><br><span class="line">    stats uri     /haproxyadmin?stats</span><br><span class="line">    stats realm   Haproxy\ Statistics</span><br><span class="line">    stats auth    admin:admin</span><br><span class="line">    stats admin <span class="keyword">if</span> TRUE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">frontend http-in</span><br><span class="line">    <span class="built_in">bind</span> *:80</span><br><span class="line">    mode http</span><br><span class="line">    <span class="built_in">log</span> global</span><br><span class="line">    option httpclose</span><br><span class="line">    option logasap</span><br><span class="line">    option dontlognull</span><br><span class="line">    capture request  header Host len 20</span><br><span class="line">    capture request  header Referer len 60</span><br><span class="line">    default_backend servers</span><br><span class="line"></span><br><span class="line">frontend healthcheck</span><br><span class="line">    <span class="built_in">bind</span> :1099</span><br><span class="line">    mode http</span><br><span class="line">    option httpclose</span><br><span class="line">    option forwardfor</span><br><span class="line">    default_backend servers</span><br><span class="line"></span><br><span class="line">backend servers</span><br><span class="line">  balance roundrobin</span><br><span class="line">    server websrv1 192.168.10.11:80 check maxconn 2000</span><br><span class="line">    server websrv2 192.168.10.12:80 check maxconn 2000</span><br></pre></td></tr></table></figure>
<h2 id="负载均衡MySQL服务的配置示例"><a href="#负载均衡MySQL服务的配置示例" class="headerlink" title="负载均衡MySQL服务的配置示例"></a>负载均衡MySQL服务的配置示例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#---------------------------------------------------------------------</span></span><br><span class="line"><span class="comment"># Global settings</span></span><br><span class="line"><span class="comment">#---------------------------------------------------------------------</span></span><br><span class="line">global</span><br><span class="line">    <span class="comment"># to have these messages end up in /var/log/haproxy.log you will</span></span><br><span class="line">    <span class="comment"># need to:</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment"># 1) configure syslog to accept network log events.  This is done</span></span><br><span class="line">    <span class="comment">#    by adding the '-r' option to the SYSLOGD_OPTIONS in</span></span><br><span class="line">    <span class="comment">#    /etc/sysconfig/syslog</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment"># 2) configure local2 events to go to the /var/log/haproxy.log</span></span><br><span class="line">    <span class="comment">#   file. A line like the following can be added to</span></span><br><span class="line">    <span class="comment">#   /etc/sysconfig/syslog</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment">#    local2.*                       /var/log/haproxy.log</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="built_in">log</span>         127.0.0.1 local2</span><br><span class="line"></span><br><span class="line">    chroot      /var/lib/haproxy</span><br><span class="line">    pidfile     /var/run/haproxy.pid</span><br><span class="line">    maxconn     4000</span><br><span class="line">    user        haproxy</span><br><span class="line">    group       haproxy</span><br><span class="line">    daemon</span><br><span class="line"></span><br><span class="line">defaults</span><br><span class="line">    mode                    tcp</span><br><span class="line">    <span class="built_in">log</span>                     global</span><br><span class="line">    option                  httplog</span><br><span class="line">    option                  dontlognull</span><br><span class="line">    retries                 3</span><br><span class="line">    timeout http-request    10s</span><br><span class="line">    timeout queue           1m</span><br><span class="line">    timeout connect         10s</span><br><span class="line">    timeout client          1m</span><br><span class="line">    timeout server          1m</span><br><span class="line">    timeout http-keep-alive 10s</span><br><span class="line">    timeout check           10s</span><br><span class="line">    maxconn                 600</span><br><span class="line"></span><br><span class="line">listen stats</span><br><span class="line">    mode http</span><br><span class="line">    <span class="built_in">bind</span> 0.0.0.0:1080</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats hide-version</span><br><span class="line">    stats uri     /haproxyadmin?stats</span><br><span class="line">    stats realm   Haproxy\ Statistics</span><br><span class="line">    stats auth    admin:admin</span><br><span class="line">    stats admin <span class="keyword">if</span> TRUE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">frontend mysql</span><br><span class="line">    <span class="built_in">bind</span> *:3306</span><br><span class="line">    mode tcp</span><br><span class="line">    <span class="built_in">log</span> global</span><br><span class="line">    default_backend mysqlservers</span><br><span class="line"></span><br><span class="line">backend mysqlservers</span><br><span class="line">    balance leastconn</span><br><span class="line">    server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300</span><br><span class="line">    server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300</span><br></pre></td></tr></table></figure>
<h1 id="ACL"><a href="#ACL" class="headerlink" title="ACL"></a>ACL</h1><h2 id="基本说明"><a href="#基本说明" class="headerlink" title="基本说明"></a>基本说明</h2><p>haproxy的ACL用于实现基于请求报文的首部、响应报文的内容或其它的环境状态信息来做出转发决策，这大大增强了其配置弹性。其配置法则通常分为两步，首先去定义ACL，即定义一个测试条件，而后在条件得到满足时执行某特定的动作，如阻止请求或转发至某特定的后端。定义ACL的语法格式如下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">acl &lt;aclname&gt; &lt;criterion&gt; [flags] [operator] &lt;value&gt; ...</span><br></pre></td></tr></table></figure>
<ul>
<li><code>&lt;aclname&gt;</code>：ACL名称，区分字符大小写，且其只能包含大小写字母、数字、-(连接线)、_(下划线)、.(点号)和:(冒号)；haproxy中，acl可以重名，这可以把多个测试条件定义为一个共同的acl；</li>
<li><code>&lt;criterion&gt;</code>：测试标准，即对什么信息发起测试；测试方式可以由[flags]指定的标志进行调整；而有些测试标准也可以需要为其在<value>之前指定一个操作符[operator]；</value></li>
<li><code>[flags]</code>：目前haproxy的acl支持的标志位有3个：</li>
<li><code>-i</code>：不区分<value>中模式字符的大小写；</value></li>
<li><code>-f</code>：从指定的文件中加载模式；</li>
<li><code>--</code>：标志符的强制结束标记，在模式中的字符串像标记符时使用；</li>
<li><code>&lt;value&gt;</code>：acl测试条件支持的值有以下四类：<ul>
<li>整数或整数范围：如1024:65535表示从1024至65535；仅支持使用正整数(如果出现类似小数的标识，其为通常为版本测试)，且支持使用的操作符有5个，分别为eq、ge、gt、le和lt；</li>
<li>字符串：支持使用“-i”以忽略字符大小写，支持使用“\”进行转义；如果在模式首部出现了-i，可以在其之前使用“–”标志位；</li>
<li>正则表达式：其机制类同字符串匹配；</li>
<li>IP地址及网络地址</li>
</ul>
</li>
</ul>
<p>同一个acl中可以指定多个测试条件，这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种：“与”(默认即为与操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。</p>
<h2 id="常用的测试标准-criteria"><a href="#常用的测试标准-criteria" class="headerlink" title="常用的测试标准(criteria)"></a>常用的测试标准(criteria)</h2><h3 id="be-sess-rate"><a href="#be-sess-rate" class="headerlink" title="be_sess_rate"></a>be_sess_rate</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">be_sess_rate(backend) &lt;<span class="built_in">integer</span>&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试指定的backend上会话创建的速率(即每秒创建的会话数)是否满足指定的条件；常用于在指定backend上的会话速率过高时将用户请求转发至另外的backend，或用于阻止攻击行为。例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">backend dynamic</span><br><span class="line">    mode http</span><br><span class="line">    acl being_scanned be_sess_rate gt 50</span><br><span class="line">    redirect location /error_pages/denied.html <span class="keyword">if</span> being_scanned</span><br></pre></td></tr></table></figure>
<h3 id="fe-sess-rate"><a href="#fe-sess-rate" class="headerlink" title="fe_sess_rate"></a>fe_sess_rate</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fe_sess_rate(frontend) &lt;<span class="built_in">integer</span>&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试指定的frontend(或当前frontend)上的会话创建速率是否满足指定的条件；常用于为frontend指定一个合理的会话创建速率的上限以防止服务被滥用。例如下面的例子限定入站邮件速率不能大于50封/秒，所有在此指定范围之外的请求都将被延时50毫秒。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">frontend mail</span><br><span class="line">    <span class="built_in">bind</span> :25</span><br><span class="line">    mode tcp</span><br><span class="line">    maxconn 500</span><br><span class="line">    acl too_fast fe_sess_rate ge 50</span><br><span class="line">    tcp-request inspect-delay 50ms</span><br><span class="line">    tcp-request content accept <span class="keyword">if</span> ! too_fast</span><br><span class="line">    tcp-request content accept <span class="keyword">if</span> WAIT_END</span><br></pre></td></tr></table></figure>
<h3 id="hdr"><a href="#hdr" class="headerlink" title="hdr"></a>hdr</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdr(header) &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试请求报文中的所有首部或指定首部是否满足指定的条件；指定首部时，其名称不区分大小写，且在括号“()”中不能有任何多余的空白字符。测试服务器端的响应报文时可以使用shdr()。例如下面的例子用于测试首部Connection的值是否为close。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdr(Connection) -i close</span><br></pre></td></tr></table></figure>
<h3 id="method"><a href="#method" class="headerlink" title="method"></a>method</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">method &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>测试HTTP请求报文中使用的方法。</p>
<h3 id="path-beg"><a href="#path-beg" class="headerlink" title="path_beg"></a>path_beg</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">path_beg &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试请求的URL是否以<string>指定的模式开头。下面的例子用于测试URL是否以/static、/images、/javascript或/stylesheets头。</string></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">acl url_static       path_beg       -i /static /images /javascript /stylesheets</span><br></pre></td></tr></table></figure>
<h3 id="path-end"><a href="#path-end" class="headerlink" title="path_end"></a>path_end</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">path_end &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试请求的URL是否以<string>指定的模式结尾。例如，下面的例子用户测试URL是否以jpg、gif、png、css或js结尾。</string></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">acl url_static       path_end       -i .jpg .gif .png .css .js</span><br></pre></td></tr></table></figure>
<h3 id="hdr-beg"><a href="#hdr-beg" class="headerlink" title="hdr_beg"></a>hdr_beg</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdr_beg &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试请求报文的指定首部的开头部分是否符合<string>指定的模式。例如，下面的例子用记测试请求是否为提供静态内容的主机img、video、download或ftp。</string></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">acl host_static hdr_beg(host) -i img. video. download. ftp.</span><br></pre></td></tr></table></figure>
<h3 id="hdr-end"><a href="#hdr-end" class="headerlink" title="hdr_end"></a>hdr_end</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdr_end &lt;string&gt;</span><br></pre></td></tr></table></figure>
<p>用于测试请求报文的指定首部的结尾部分是否符合<string>指定的模式。例如，下面的例子用记测试请求是否为动静分离示例：</string></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">global</span><br><span class="line">    <span class="built_in">log</span>         127.0.0.1 local2</span><br><span class="line"></span><br><span class="line">    chroot      /var/lib/haproxy</span><br><span class="line">    pidfile     /var/run/haproxy.pid</span><br><span class="line">    maxconn     4000</span><br><span class="line">    user        haproxy</span><br><span class="line">    group       haproxy</span><br><span class="line">    daemon</span><br><span class="line"></span><br><span class="line">    <span class="comment"># turn on stats unix socket</span></span><br><span class="line">    stats socket /var/lib/haproxy/stats</span><br><span class="line"></span><br><span class="line">defaults</span><br><span class="line">    mode                    http</span><br><span class="line">    <span class="built_in">log</span>                     global</span><br><span class="line">    option                  httplog</span><br><span class="line">    option                  dontlognull</span><br><span class="line">    option http-server-close</span><br><span class="line">    option forwardfor       except 127.0.0.0/8</span><br><span class="line">    option                  redispatch</span><br><span class="line">    retries                 3</span><br><span class="line">    timeout http-request    10s</span><br><span class="line">    timeout queue           1m</span><br><span class="line">    timeout connect         10s</span><br><span class="line">    timeout client          1m</span><br><span class="line">    timeout server          1m</span><br><span class="line">    timeout http-keep-alive 10s</span><br><span class="line">    timeout check           10s</span><br><span class="line">    maxconn                 30000</span><br><span class="line"></span><br><span class="line">listen stats</span><br><span class="line">    mode http</span><br><span class="line">    <span class="built_in">bind</span> 0.0.0.0:1080</span><br><span class="line">    stats <span class="built_in">enable</span></span><br><span class="line">    stats hide-version</span><br><span class="line">    stats uri     /haproxyadmin?stats</span><br><span class="line">    stats realm   Haproxy\ Statistics</span><br><span class="line">    stats auth    admin:admin</span><br><span class="line">    stats admin <span class="keyword">if</span> TRUE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">frontend http-in</span><br><span class="line">    <span class="built_in">bind</span> *:80</span><br><span class="line">    mode http</span><br><span class="line">    <span class="built_in">log</span> global</span><br><span class="line">    option httpclose</span><br><span class="line">    option logasap</span><br><span class="line">    option dontlognull</span><br><span class="line">    capture request  header Host len 20</span><br><span class="line">    capture request  header Referer len 60</span><br><span class="line">    acl url_static       path_beg       -i /static /images /javascript /stylesheets</span><br><span class="line">    acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js</span><br><span class="line"></span><br><span class="line">    use_backend static_servers          <span class="keyword">if</span> url_static</span><br><span class="line">    default_backend dynamic_servers</span><br><span class="line"></span><br><span class="line">backend static_servers</span><br><span class="line">    balance roundrobin</span><br><span class="line">    server imgsrv1 172.16.200.7:80 check maxconn 6000</span><br><span class="line">    server imgsrv2 172.16.200.8:80 check maxconn 6000</span><br><span class="line"></span><br><span class="line">backend dynamic_servers</span><br><span class="line">    balance <span class="built_in">source</span></span><br><span class="line">    server websrv1 172.16.200.7:80 check maxconn 1000</span><br><span class="line">    server websrv2 172.16.200.8:80 check maxconn 1000</span><br><span class="line">    server websrv3 172.16.200.9:80 check maxconn 1000</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>有钱任性，请我吃包辣条</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="孟庆宇 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="孟庆宇 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    孟庆宇
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://linuxgeeks.github.io/2017/12/04/2017-12-04-223110-Haproxy配置文件参数说明/" title="Haproxy配置文件参数说明">http://linuxgeeks.github.io/2017/12/04/2017-12-04-223110-Haproxy配置文件参数说明/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Haproxy/" rel="tag"><i class="fa fa-tag"></i> Haproxy</a>
          
            <a href="/tags/负载均衡/" rel="tag"><i class="fa fa-tag"></i> 负载均衡</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/12/03/2017-12-03-082510-Haproxy配置实现Web负载均衡/" rel="next" title="Haproxy配置实现Web负载均衡">
                <i class="fa fa-chevron-left"></i> Haproxy配置实现Web负载均衡
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/12/05/2017-12-05-082710-Haproxy实现状态监控页面/" rel="prev" title="Haproxy实现状态监控页面">
                Haproxy实现状态监控页面 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "16",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "16"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          
  


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="孟庆宇" />
            
              <p class="site-author-name" itemprop="name">孟庆宇</p>
              <p class="site-description motion-element" itemprop="description">书山有路勤为径，学海无涯苦作舟</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">109</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">23</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">56</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/mengdaya" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:yourname@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.ibm.com/developerworks/cn/" title="IBM developerWorks" target="_blank">IBM developerWorks</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://iissnan.com/progit/" title="Pro Git" target="_blank">Pro Git</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.freeoa.net/index/" title="freeOA" target="_blank">freeOA</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.ttlsa.com/" title="运维生存时间" target="_blank">运维生存时间</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.361way.com/" title="运维之路" target="_blank">运维之路</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.yiibai.com/" title="易百教程" target="_blank">易百教程</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.centoscn.com/" title="CentOS中文站" target="_blank">CentOS中文站</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.linuxprobe.com/" title="Linux就该这么学" target="_blank">Linux就该这么学</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.netingcn.com/" title="网络进行时" target="_blank">网络进行时</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.ruanyifeng.com/blog/" title="阮一峰的网络日志" target="_blank">阮一峰的网络日志</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.liaoxuefeng.com" title="廖雪峰的官方网站" target="_blank">廖雪峰的官方网站</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yihui.name/cn/" title="谢益辉的博客" target="_blank">谢益辉的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://coolshell.cn" title="酷壳" target="_blank">酷壳</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#配置HAProxy"><span class="nav-number">1.</span> <span class="nav-text">配置HAProxy</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#配置文件格式"><span class="nav-number">1.1.</span> <span class="nav-text">配置文件格式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#时间格式"><span class="nav-number">1.1.1.</span> <span class="nav-text">时间格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#例子"><span class="nav-number">1.1.2.</span> <span class="nav-text">例子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#全局配置"><span class="nav-number">1.2.</span> <span class="nav-text">全局配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#进程管理及安全相关的参数"><span class="nav-number">1.2.1.</span> <span class="nav-text">进程管理及安全相关的参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#性能调整相关的参数"><span class="nav-number">1.2.2.</span> <span class="nav-text">性能调整相关的参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Debug相关的参数"><span class="nav-number">1.2.3.</span> <span class="nav-text">Debug相关的参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代理"><span class="nav-number">1.3.</span> <span class="nav-text">代理</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#配置文件中的关键字参考"><span class="nav-number">2.</span> <span class="nav-text">配置文件中的关键字参考</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#balance"><span class="nav-number">2.1.</span> <span class="nav-text">balance</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#bind"><span class="nav-number">2.2.</span> <span class="nav-text">bind</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mode"><span class="nav-number">2.3.</span> <span class="nav-text">mode</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#hash-type"><span class="nav-number">2.4.</span> <span class="nav-text">hash-type</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#log"><span class="nav-number">2.5.</span> <span class="nav-text">log</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#maxconn"><span class="nav-number">2.6.</span> <span class="nav-text">maxconn</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#default-backend"><span class="nav-number">2.7.</span> <span class="nav-text">default_backend</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#server"><span class="nav-number">2.8.</span> <span class="nav-text">server</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#capture-request-header"><span class="nav-number">2.9.</span> <span class="nav-text">capture request header</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#capture-response-header"><span class="nav-number">2.10.</span> <span class="nav-text">capture response header</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-enable"><span class="nav-number">2.11.</span> <span class="nav-text">stats enable</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-hide-version"><span class="nav-number">2.12.</span> <span class="nav-text">stats hide-version</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-realm"><span class="nav-number">2.13.</span> <span class="nav-text">stats realm</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-scope"><span class="nav-number">2.14.</span> <span class="nav-text">stats scope</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-auth"><span class="nav-number">2.15.</span> <span class="nav-text">stats auth</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stats-admin"><span class="nav-number">2.16.</span> <span class="nav-text">stats admin</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#option-httplog"><span class="nav-number">2.17.</span> <span class="nav-text">option httplog</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#option-logasap"><span class="nav-number">2.18.</span> <span class="nav-text">option logasap</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#option-forwardfor"><span class="nav-number">2.19.</span> <span class="nav-text">option forwardfor</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#errorfile"><span class="nav-number">2.20.</span> <span class="nav-text">errorfile</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#errorloc-和-errorloc302"><span class="nav-number">2.21.</span> <span class="nav-text">errorloc 和 errorloc302</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#errorloc303"><span class="nav-number">2.22.</span> <span class="nav-text">errorloc303</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#配置案例"><span class="nav-number">3.</span> <span class="nav-text">配置案例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#http服务器配置示例"><span class="nav-number">3.1.</span> <span class="nav-text">http服务器配置示例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#负载均衡MySQL服务的配置示例"><span class="nav-number">3.2.</span> <span class="nav-text">负载均衡MySQL服务的配置示例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ACL"><span class="nav-number">4.</span> <span class="nav-text">ACL</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本说明"><span class="nav-number">4.1.</span> <span class="nav-text">基本说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用的测试标准-criteria"><span class="nav-number">4.2.</span> <span class="nav-text">常用的测试标准(criteria)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#be-sess-rate"><span class="nav-number">4.2.1.</span> <span class="nav-text">be_sess_rate</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fe-sess-rate"><span class="nav-number">4.2.2.</span> <span class="nav-text">fe_sess_rate</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hdr"><span class="nav-number">4.2.3.</span> <span class="nav-text">hdr</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#method"><span class="nav-number">4.2.4.</span> <span class="nav-text">method</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#path-beg"><span class="nav-number">4.2.5.</span> <span class="nav-text">path_beg</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#path-end"><span class="nav-number">4.2.6.</span> <span class="nav-text">path_end</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hdr-beg"><span class="nav-number">4.2.7.</span> <span class="nav-text">hdr_beg</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hdr-end"><span class="nav-number">4.2.8.</span> <span class="nav-text">hdr_end</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文章总结于网络，转载请注明出处</span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="true"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  








  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  

  

  

</body>
</html>
